﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MyCtp.Api;
using MyCtp.Model;

using TradeCore.Model;
using TradeCore.Quote;

namespace TradeCore.Quote
{
    public class CtpQuote
    {
        private QuoteManager quoteManager;

        private MdApi mdApi;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="account">CTP账号</param>
        /// <param name="mdFront">行情前置机</param>
        /// <param name="subscribeInstruments">订阅行情的合约</param>
        public CtpQuote(CtpAccount account, string[] mdFront, string[] subscribeInstruments)
        {
            quoteManager = QuoteManager.GetInstance();

            mdApi = new MdApi(account.BrokerID, account.Account, account.Password, mdFront, subscribeInstruments);
            mdApi.LastPriceArrive += OnLastPriceArrive;
        }

        /// <summary>`
        /// 将最新行情数据放入队列
        /// </summary>
        /// <param name="code">代码</param>
        /// <param name="price">最新价格</param>
        /// <param name="updateTime">更新时间</param>
        private void OnLastPriceArrive(string code, double price, DateTime updateTime)
        {
            QueueQuoteData quote = new QueueQuoteData();

            quote.Market     = MarketType.Futures;
            quote.Code       = code;
            quote.Price      = price;
            quote.UpdateTime = updateTime;

            quoteManager.EnqueueQuote(quote);
        }
    }
}
